package aof.controller;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import aof.biz.L_COSTTYPE_BIZ;
import aof.helper.DateTimeHelper;
import aof.model.L_COSTTYPE;
import aof.model.M_COSTDAY;

/**
 * Created by AOF on 2/6/2556.
 */
public class AddCostController extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcost);

        Button txtAddType = (Button)findViewById(R.id.btnAddType);
        txtAddType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog dialog = new Dialog(AddCostController.this,R.style.DialogSlideAnim);

                dialog.setContentView(R.layout.dialogtype);
                Window window = dialog.getWindow();
                WindowManager.LayoutParams wlp = window.getAttributes();
                wlp.width = AddCostController.this.getWindowManager().getDefaultDisplay().getWidth();
                wlp.gravity = Gravity.BOTTOM;
                wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                window.setAttributes(wlp);
                dialog.setCancelable(true);

                ListView lv = (ListView)dialog.findViewById(R.id.listViewType);
                lv.setAdapter(new ViewAdapter(AddCostController.this,R.layout._column,new L_COSTTYPE_BIZ(AddCostController.this).GetAll()));
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        ViewAdapter adt = (ViewAdapter)adapterView.getAdapter();
                        L_COSTTYPE data = adt.getItem(i);

                        toastShow( "Selected : " + data.getNAME_TH());


                        Button btntype = (Button)findViewById(R.id.btnAddType);
                        btntype.setText(data.getNAME_TH());

                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });

        ImageButton btnCancel = (ImageButton)findViewById(R.id.btnAddCencel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Intent newActivity = new Intent(getApplicationContext(), MainController.class);
            startActivity(newActivity);
            overridePendingTransition(R.anim.right_to_left_enter, R.anim.right_to_left_leave);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main_controller, menu);
        return true;
    }
    @Override
    public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called by Add Cost");
        Intent newActivity = new Intent(getApplicationContext(), MainController.class);
        startActivity(newActivity);
        overridePendingTransition(R.anim.right_to_left_enter, R.anim.right_to_left_leave);
    }

    public void toastShow(String txt)
    {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout._toast,(ViewGroup) findViewById(R.id.custom_toast_layout_id));
        TextView text = (TextView) layout.findViewById(R.id.toast_text);
        text.setText(txt);
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

    public class ViewAdapter extends ArrayAdapter<L_COSTTYPE> {

        private Context context;
        private ArrayList<L_COSTTYPE> Myitems;

        public ViewAdapter(Context context, int textViewResourceId, ArrayList<L_COSTTYPE> items) {
            super(context, textViewResourceId, items);
            this.context = context;
            this.Myitems = new ArrayList<L_COSTTYPE>();
            this.Myitems.addAll(items);
        }
        class ViewHolder {
            TextView txtNumber;
            TextView txtValue;
        }
        public void add(L_COSTTYPE item){
            this.Myitems.add(item);
        }

        @Override
        public View getView(final int position, View convertView,ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            ViewHolder holder = null;

            if (convertView == null) {
                convertView = inflater.inflate(R.layout._column, null);
                holder = new ViewHolder();
                holder.txtNumber = (TextView) convertView.findViewById(R.id.txtTypeRowNum);
                holder.txtValue = (TextView) convertView.findViewById(R.id.txtTypeName);
                convertView.setTag(holder);

            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }

            holder.txtNumber.setText(this.Myitems.get(position).getROWNUM().toString());
            holder.txtValue.setText(this.Myitems.get(position).getNAME_TH().toString());

            return convertView;

        }

    }


}
